package com.yanqu.road.web.mgr;

import com.yanqu.road.entity.activity.SimpleActivityInfo;
import com.yanqu.road.logic.bussiness.DaoHelper;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;
import com.yanqu.road.web.WebServer;
import com.yanqu.road.web.entity.AllActivity;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class AllActivityMainInfoMgr extends TempMgr {

    public static AllActivity allActivity;

    //type serverList2 activityList
    public static Map<Integer, Map<String, List<Integer>>> serverList2Map;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        reloadActivityData();
        return true;
    }

    public static void reloadActivityData() {
        if (WebServer.getInstance().isUserCenter()) {
            Map<Integer, List<SimpleActivityInfo>> serverIdActivityInfoMap = new ConcurrentHashMap<>();
            Map<Integer, List<SimpleActivityInfo>> typeActivityInfoMap = new ConcurrentHashMap<>();
            List<SimpleActivityInfo> simpleActivityInfoList = DaoHelper.getActivityDaoImpl().getAllActivityList();
            for (SimpleActivityInfo simpleActivityInfo : simpleActivityInfoList) {
                for (int serverId : simpleActivityInfo.getServerIdList()) {
                    if (!serverIdActivityInfoMap.containsKey(serverId)) {
                        serverIdActivityInfoMap.put(serverId, new ArrayList<>());
                    }
                    serverIdActivityInfoMap.get(serverId).add(simpleActivityInfo);
                }

                if (!typeActivityInfoMap.containsKey(simpleActivityInfo.getType())) {
                    typeActivityInfoMap.put(simpleActivityInfo.getType(), new ArrayList<>());
                }
                typeActivityInfoMap.get(simpleActivityInfo.getType()).add(simpleActivityInfo);
            }

            AllActivity allActivityTemp = new AllActivity(serverIdActivityInfoMap, typeActivityInfoMap);
            allActivity = allActivityTemp;

            Map<Integer, Map<String, List<Integer>>> tempMap = new ConcurrentHashMap<>();
            int currentSecond = DateHelper.getCurrentSecond() + (int)DateHelper.HOUR_SECONDS;
            for (Map.Entry<Integer, List<SimpleActivityInfo>> entry : allActivityTemp.getTypeActivityInfoMap().entrySet()) {
                for (SimpleActivityInfo info : entry.getValue()) {
                    if(info.getEndShowTime() < currentSecond){
                        continue;
                    }
                    if(info.getServerIdList2().isEmpty() || info.getServerIdList2().size() == 1){
                        continue;
                    }
                    String str = StringUtils.listToString(info.getServerIdList2(), ",");
                    if(!tempMap.containsKey(info.getType())){
                        tempMap.put(info.getType(), new ConcurrentHashMap<>());
                    }
                    if(!tempMap.get(info.getType()).containsKey(str)){
                        tempMap.get(info.getType()).put(str, new ArrayList<>());
                    }
                    tempMap.get(info.getType()).get(str).add(info.getActivityId());
                }
            }
            serverList2Map = tempMap;
            getLogger().debug("加载所有活动完成");
        }
    }

    /**
     * 获取区服活动列表ID
     * @return
     */
    public static List<Integer> getActivityIdListByServerId(int serverId, long serverTime) {
        if (allActivity == null) {
            return new ArrayList<>();
        }

        return allActivity.getActivityIdListByServerId(serverId, serverTime);
    }

    /**
     * 获取区服列表活动ID
     * @return
     */
    public static List<Integer> getActivityIdListByServerIdAndType(int serverId, List<Integer> types, long serverTime) {
        if (allActivity == null) {
            return new ArrayList<>();
        }
        return allActivity.getActivityIdListByServerIdAndType(serverId, types, serverTime);
    }

    /**
     * 获取参与范围一致的区服活动列表ID
     * @return
     */
    public static Map<Integer, List<Integer>> getActivityIdListByServerId(int serverId) {
        Map<Integer, List<Integer>> map = new HashMap<>();
        for (Map.Entry<Integer, Map<String, List<Integer>>> entry : serverList2Map.entrySet()) {
            int type = entry.getKey();
            for (Map.Entry<String, List<Integer>> listEntry : entry.getValue().entrySet()) {
                List<Integer> list = StringUtils.stringToIntegerList(listEntry.getKey(), ",");
                if(list.contains(serverId)){
                    List<Integer> value = listEntry.getValue();
                    if(!value.isEmpty()) {
                        map.put(type, value);
                    }
                }
            }
        }
        return map;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
